package com.px.service;

import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.px.client.UserFeignClient;
import com.px.common.Constants;
import com.px.dto.ReturnResult;
import com.px.dto.ReturnResultUtils;
import com.px.pojo.ShUser;
import com.px.utils.*;
import com.px.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserFeignClient userFeignClient;
    @Resource
    private ObjectMapper objectMapper;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 注册
     * @param user
     * @return
     * @throws Exception
     */
    @Override
    public ReturnResult login( ShUser user) throws Exception {
        ShUser shUser = new ShUser();
        shUser.setCreatedTime(new Date());
        BeanUtils.copyProperties(shUser,user);
        boolean b = userFeignClient.insertUser(shUser);
        if (b){
            return ReturnResultUtils.returnSuccess();
        }
        return ReturnResultUtils.returnFail(1002,"失败");
    }

    /**
     * 登录
     * @param account
     * @param password
     * @return
     * @throws Exception
     */
    @Override
    public ReturnResult denglu(String account,  String password) throws Exception {
        String token = null;

        ShUser listMapUser = userFeignClient.getListMapUser(account);

        String md5Password = MD5Utils.getMd5(password,32);
        if (md5Password.equals(listMapUser.getPassword())) {
            Integer userid =  listMapUser.getUserid();

            if (listMapUser != null){
                //判断redis中是否有用户id
                String str = redisUtil.getStr(userid.toString());
                if (EmptyUtils.isNotEmpty(str)){
                    //清除旧的token
                    redisUtil.del(str);
                }
                //生成一个新的token
                token = Constants.tokenPrefix + TokenUtils.createToken(userid.toString(),listMapUser.getAccount());
                //存入redis
                //将java对象序列化为json
                String json = objectMapper.writeValueAsString(listMapUser);
                redisUtil.setStr(token,json);
                redisUtil.setStr(userid.toString(),token);

                Map<String,Object> map = new HashMap<>();
                map.put("token",token);
                return ReturnResultUtils.returnSuccess(map);
            }
            return ReturnResultUtils.returnFail(1001,"登录失败");
        }

        return ReturnResultUtils.returnFail(1001,"密码错误");
    }

    /**
     * 退出
     * @param
     * @return
     * @throws Exception
     */
    @Override
    public ReturnResult deleteToken(String token) throws Exception {


        String json = redisUtil.getStr(token);
        //使用jackson解析技术将json串反序列化为java对象
        ShUser qgUser = objectMapper.readValue(json, ShUser.class);
        //移除
        redisUtil.del(qgUser.getUserid().toString());
        redisUtil.del(token);

        return ReturnResultUtils.returnSuccess();
    }
}
